package com.thinkgem.jeesite.modules.cms.web.front;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.thinkgem.jeesite.modules.university.entity.DisciplineFields;
import com.thinkgem.jeesite.modules.university.entity.XueLiLevel;
import com.thinkgem.jeesite.modules.university.entity.Major;
import com.thinkgem.jeesite.modules.university.entity.MajorCategory;
import com.thinkgem.jeesite.modules.university.service.DisciplineFieldsService;
import com.thinkgem.jeesite.modules.university.service.XueLiLevelService;
import com.thinkgem.jeesite.modules.university.service.MajorCategoryService;
import com.thinkgem.jeesite.modules.university.service.MajorService;
/**
 * 专业Controller
 * @author 朱俊吉
 * @version 2017-11-25
 */
@Controller
@RequestMapping(value = "${frontPath}/major")
public class FrontMajorController {
	
	@Autowired
	private XueLiLevelService xueLiLevelService;
	@Autowired
	private DisciplineFieldsService disciplineFieldsService;
	@Autowired
	private MajorCategoryService majorCategoryService;
	@Autowired
	private MajorService majorService;
	
	/**
	 * 专业大全
	 * @param article
	 * @return
	 */
	@RequestMapping(value = "complete-{cengci}-{menlei}-{leibie}${urlSuffix}", method = RequestMethod.GET)
    private String complete(
    		@PathVariable String cengci, 
    		@PathVariable String menlei, 
    		@PathVariable String leibie, 
    		Model model, HttpServletRequest request){
		
		XueLiLevel xueLi = new XueLiLevel();
		DisciplineFields df = new DisciplineFields();
		MajorCategory mc = new MajorCategory();
		Major major = new Major();
		List<XueLiLevel> cengciList = new ArrayList<>();
		List<DisciplineFields> menleiList = new ArrayList<>();
		List<MajorCategory> leibieList = new ArrayList<>();
		List<Major> majorList = new ArrayList<>();
		
		cengciList = xueLiLevelService.findList(xueLi);
		
		//没选择学历默认选中本科
		if(StringUtils.isBlank(cengci)){
			
			cengciList = xueLiLevelService.findList(xueLi);
			//学历集合为空不查询学科门类和专业类别集合
			if(cengciList != null && cengciList.size() > 0 ){
				xueLi = cengciList.get(0);
				df.setXueLiLevel(xueLi);
				menleiList = disciplineFieldsService.findList(df);
				//如果学科门类集合为空不查询专业类别集合
				if(menleiList != null && menleiList.size() > 0 ){
					df = menleiList.get(0);
					mc.setDisciplineFields(df);
					leibieList = majorCategoryService.findList(mc);
					//如果专业列别集合为空
					if(leibieList != null && leibieList.size() > 0 ){
						mc = leibieList.get(0);
						major.setMajorCategory(mc);
						majorList = majorService.findList(major);
					}
				}
			}
			
		}
		
		//选择学历了
		if(StringUtils.isNoneBlank(cengci)){
			xueLi = xueLiLevelService.get(cengci);
			df.setXueLiLevel(xueLi);
			menleiList = disciplineFieldsService.findList(df);
			
			//没有选择学科门类,且学科门类集合不会空
			if(StringUtils.isBlank(menlei) && menleiList != null && menleiList.size() > 0){
				df = menleiList.get(0);
				mc.setDisciplineFields(df);
				leibieList = majorCategoryService.findList(mc);
				//如果专业列别集合为空
				if(leibieList != null && leibieList.size() > 0 ){
					mc = leibieList.get(0);
					major.setMajorCategory(mc);
					majorList = majorService.findList(major);
				}
			}
			//选择了学科门类
			if(StringUtils.isNoneBlank(menlei)){
				df = disciplineFieldsService.get(menlei);
				mc.setDisciplineFields(df);
				leibieList = majorCategoryService.findList(mc);
				//没有选择专业类别,切专业类别集合不会空
				if(StringUtils.isBlank(leibie) && leibieList != null && leibieList.size() > 0){
					mc = leibieList.get(0);
					major.setMajorCategory(mc);
					majorList = majorService.findList(major);
				}
				//选择了专业类别
				if(StringUtils.isNoneBlank(leibie)){
					mc = majorCategoryService.get(leibie);
					major.setMajorCategory(mc);
					majorList = majorService.findList(major);
				}
			}
		}
		
		model.addAttribute("xueLi", xueLi);
		model.addAttribute("disciplineFields", df);
		model.addAttribute("majorCategory", mc);
		model.addAttribute("cengciList", cengciList);
		model.addAttribute("menleiList", menleiList);
		model.addAttribute("leibieList", leibieList);
		model.addAttribute("majorList", majorList);
		
       return "modules/cms/front/themes/wending/major_complete";
    }
	
	/**
	 * 专业解读
	 */
	@RequestMapping(value = "read-{id}${urlSuffix}", method = RequestMethod.GET)
    private String read(@PathVariable String id, Model model, HttpServletRequest request){
		Major major = majorService.get(id);
		model.addAttribute("major", major);
		//页面样式专业解读选中
		model.addAttribute("mPage","1");
		return "modules/cms/front/themes/wending/major_read";
	}
	/**
	 * 开设院校
	 */
	@RequestMapping(value = "openUniversities-{id}${urlSuffix}", method = RequestMethod.GET)
    private String openUniversities(@PathVariable String id, Model model, HttpServletRequest request){
		Major major = majorService.get(id);
		model.addAttribute("major", major);
		//页面央视开设院校选中
		model.addAttribute("mPage","2");
		return "modules/cms/front/themes/wending/major_openUniversities";
	}
	public static void main(String[] args) {
		String str = "-";
		System.out.println(str);
	}
	/**
	 * 专业排名
	 */
	@RequestMapping(value = "ranking-{id}${urlSuffix}", method = RequestMethod.GET)
    private String ranking(@PathVariable String id, Model model, HttpServletRequest request){
		Major major = majorService.get(id);
		model.addAttribute("major", major);
		//页面样式专业排名选中
		model.addAttribute("mPage","3");
		return "modules/cms/front/themes/wending/major_ranking";
	}
	/**
	 * 招生计划
	 */
	@RequestMapping(value = "plan-{id}${urlSuffix}", method = RequestMethod.GET)
    private String plan(@PathVariable String id, Model model, HttpServletRequest request){
		Major major = majorService.get(id);
		model.addAttribute("major", major);
		//页面样式招生计划选中
		model.addAttribute("mPage","4");
		return "modules/cms/front/themes/wending/major_plan";
	}
}
